package com.bichengcao.googlecodejam.round1b2008;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CropTriangles {

	static int N;

	public static void main(String[] args) throws IOException {
		Input[] inputs = parseInput("src/com/bichengcao/googlecodejam/round1b2008/A-large-practice.in");
		Output[] outputs = process(inputs);
		writeOutput("src/com/bichengcao/googlecodejam/round1b2008/CropTriangles.out", outputs);
	}

	static class Input {

		int n;
		int a, b, c, d;
		int x0, y0;
		int m;
	}

	static class Output {

		long result;
	}

	private static Input[] parseInput(String filename) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(filename));
		N = Integer.parseInt(in.readLine());
		Input[] array = new Input[N];

		for (int i = 0; i < N; i++) {
			Input input = array[i] = new Input();
			String line = in.readLine();
			String[] splits = line.split(" ");
			input.n = Integer.parseInt(splits[0]);
			input.a = Integer.parseInt(splits[1]);
			input.b = Integer.parseInt(splits[2]);
			input.c = Integer.parseInt(splits[3]);
			input.d = Integer.parseInt(splits[4]);
			input.x0 = Integer.parseInt(splits[5]);
			input.y0 = Integer.parseInt(splits[6]);
			input.m = Integer.parseInt(splits[7]);
		}

		in.close();
		return array;
	}

	private static Output[] process(Input[] inputs) {
		Output[] outputs = new Output[N];
		for (int i = 0; i < N; i++) {
			outputs[i] = process(inputs[i]);
		}
		return outputs;
	}

	private static Output process(Input input) {
		Output output = new Output();

		int[][] array = new int[3][3];
		int x = input.x0;
		int y = input.y0;
		array[x % 3][y % 3]++;
		for (int i = 1; i < input.n; i++) {
			x = (int) (((((long) input.a) * x) + input.b) % input.m);
			y = (int) (((((long) input.c) * y) + input.d) % input.m);
			array[x % 3][y % 3]++;
		}

		for (int i = 0; i < 9; i++) {
			for (int j = i; j < 9; j++) {
				int a = (9 - (i / 3 + j / 3)) % 3;
				int b = (9 - (i % 3 + j % 3)) % 3;
				if (a * 3 + b == j) {
					if (i == j) {
						output.result += ((long) array[a][b]) * (array[a][b] - 1)
								* (array[a][b] - 2) / 6;
					} else {
						output.result += ((long) array[i / 3][i % 3]) * array[a][b]
								* (array[a][b] - 1) / 2;
					}
				} else if (a * 3 + b > j) {
					if (i == j) {
						output.result += ((long) array[i / 3][i % 3]) * (array[i / 3][i % 3] - 1)
								/ 2 * array[a][b];
					} else {
						output.result += ((long) array[i / 3][i % 3]) * array[j / 3][j % 3]
								* array[a][b];
					}
				}
			}
		}

		return output;
	}

	private static void writeOutput(String filename, Output[] outputs) throws IOException {
		PrintWriter out = new PrintWriter(new FileWriter(filename));
		for (int i = 0; i < N; i++) {
			out.println("Case #" + (i + 1) + ": " + outputs[i].result);
		}
		out.close();
	}

}
